package com.tianyi.classify;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tianyi.dao.entity.classify.Classify;
import com.tianyi.dao.entity.classify.ClassifyExample;
import com.tianyi.dao.mapper.ClassifyMapper;
import com.tianyi.lib.model.WebResult;
import com.tianyi.lib.req.ClassifyReq;
import com.tianyi.sysUser.SysUserService;
import com.tianyi.vo.PullDownVO;
import com.tianyi.vo.classify.ClassifyVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 分类service
 *
 * @author cuixuhui
 * @date 2020/06/04
 */
@Service
public class ClassifyService {

    private static final Logger logger = LoggerFactory.getLogger(ClassifyService.class);

    @Autowired
    private ClassifyMapper classifyMapper;
    @Autowired
    private SysUserService sysUserService;

    public WebResult<String> preCheck(ClassifyReq req){

        if (StringUtils.isBlank(req.getClassifyName())){
            return WebResult.error("分类名称不能为空");
        }
        if (StringUtils.isBlank(req.getClassifyCode())){
            return WebResult.error("分类code不能为空");
        }

        ClassifyExample example = new ClassifyExample();
        example.createCriteria().andClassifyNameEqualTo(req.getClassifyName());
        example.or().andClassifyCodeEqualTo(req.getClassifyCode());
        List<Classify> classifies = classifyMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(classifies)){
            return WebResult.error("分类已存在");
        }

        return WebResult.ok("success");
    }

    public Classify req2Classify(ClassifyReq req, Classify updateClassify){

        Classify classify = updateClassify;
        if (Objects.isNull(classify)){
            classify = new Classify();
            classify.setGmtCreate(new Date());
        }
        classify.setGmtModified(new Date());

        classify.setClassifyName(req.getClassifyName());
        classify.setClassifyCode(req.getClassifyCode());
        classify.setClassifyContent(req.getClassifyContent());

        classify.setOperator(req.getEmpId());
        return classify;
    }

    public WebResult<String> addClassify(ClassifyReq req) {

        WebResult<String> preCheck = preCheck(req);
        if (!preCheck.isSuccess()){
            return preCheck;
        }

        Classify classify = req2Classify(req, null);
        int i = classifyMapper.insert(classify);

        return  WebResult.ok("success");
    }

    public WebResult<String> deleteClassifyById(ClassifyReq req) {

        return  WebResult.ok("success");
    }

    public WebResult<String> updateClassify(ClassifyReq req) {

        if (Objects.isNull(req.getId())){
            return WebResult.error("id 不能为空");
        }
        WebResult<String> preCheck = preCheck(req);
        if (!preCheck.isSuccess()){
            return preCheck;
        }
        Classify classify = queryClassifyByClassifyId(req.getId());
        if (Objects.isNull(classify)){
            return WebResult.error("分类不存在");
        }
        classify = req2Classify(req, classify);
        int i = classifyMapper.updateByPrimaryKeySelective(classify);

        return WebResult.ok("success");
    }

    public WebResult<PageInfo<ClassifyVO>> queryClassifyPage(ClassifyReq req) {

        ClassifyExample example = new ClassifyExample();
        example.setOrderByClause("id DESC");
        ClassifyExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(req.getClassifyName())){
            criteria.andClassifyNameEqualTo(req.getClassifyName());
        }
        if (StringUtils.isNotBlank(req.getClassifyCode())){
            criteria.andClassifyCodeEqualTo(req.getClassifyCode());
        }
        Page<Classify> classifyPage = classifyMapper.selectByExample(example, new RowBounds(req.getPageNo(), req.getPageSize()));
        PageInfo<Classify> classifyPageInfo = classifyPage.toPageInfo();
        List<Classify> classifyPageInfoList = classifyPageInfo.getList();

        PageInfo<ClassifyVO> pageInfo = new PageInfo();
        BeanUtils.copyProperties(classifyPageInfo, pageInfo);
        if (CollectionUtils.isEmpty(classifyPageInfoList)){
            return WebResult.ok(pageInfo);
        }

        List<Long> empIds = classifyPageInfoList.stream().map(Classify::getOperator).collect(Collectors.toList());
        Map<Long, String> sysUserNameMap = sysUserService.queryUserNameMapByUserIds(empIds);

        List<ClassifyVO> classifyVOS = classifyPageInfoList.stream().map(ret -> {
            ClassifyVO classifyVO = new ClassifyVO();
            BeanUtils.copyProperties(ret, classifyVO);
            classifyVO.setOperatorCn(sysUserNameMap.get(ret.getOperator()));
            return classifyVO;
        }).collect(Collectors.toList());
        pageInfo.setList(classifyVOS);
        return WebResult.ok(pageInfo);
    }


    public Map<Long, String> queryClassifyNameMapByBrandIds(List<Long> classifyIds) {

        ClassifyExample example = new ClassifyExample();
        ClassifyExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(classifyIds);

        List<Classify> classifies = classifyMapper.selectByExample(example);
        Map<Long, String> classifyIdNameMap = ListUtils.emptyIfNull(classifies).stream().collect(Collectors.toMap(Classify::getId, Classify::getClassifyName, (oldOne, newOne) -> newOne));

        return classifyIdNameMap;
    }

    public Classify queryClassifyByClassifyId(Long classifyId) {

        if (Objects.isNull(classifyId)){
            return null;
        }

        Classify classify = classifyMapper.selectByPrimaryKey(classifyId);
        return classify;
    }

    /**
     * 分类下拉
     * @param req
     * @return
     */
    public WebResult<List<PullDownVO>> getClassifyPullDown(ClassifyReq req) {

        ClassifyExample example = new ClassifyExample();
        ClassifyExample.Criteria criteria = example.createCriteria();

        List<Classify> classifies = classifyMapper.selectByExample(example);
        List<PullDownVO> collect = ListUtils.emptyIfNull(classifies).stream().map(ret -> {
            PullDownVO vo = new PullDownVO();
            vo.setLabel(ret.getClassifyName());
            vo.setValue(ret.getId());
            return vo;
        }).collect(Collectors.toList());

        return WebResult.ok(collect);
    }
}
